package com.ficus.road.maintain.service.smartnew;

import com.ficus.road.maintain.core.model.smart.HighwayRanktable;
import com.ficus.road.maintain.core.model.smartnew.SmartRankTable;
import com.ficus.road.maintain.dao.smartnew.SmartRankTableDao;
import com.ficus.road.maintain.util.MyDateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/3/2 下午7:14
 */
@Service
@RequiredArgsConstructor
public class SmartRankTableService {
    private final SmartRankTableDao smartRankTableDao;

    public List<SmartRankTable> findByRoadType(Query query) {
        // todo: 找出待排分路段(排分表)
        query.addCriteria(Criteria.where("date").is(MyDateUtil.dateFormatStr(new Date(), "yyyy-MM-dd")));
        return smartRankTableDao.findByQuery(query);
    }

    public SmartRankTable saveOrUpdate(SmartRankTable smartRankTable) {
        return smartRankTableDao.saveOrUpdate(smartRankTable);
    }

    public HighwayRanktable convert2HighwayRankTable(SmartRankTable smartRankTable) {
        return HighwayRanktable.builder()
                .id(smartRankTable.getId())
                .road(smartRankTable.getRoadName())
                .roadType(smartRankTable.getRoadCharacter())
                .bigType(smartRankTable.getBigType())
                .pci(smartRankTable.getPCI())
                .rqi(smartRankTable.getRQI())
                .pssi(smartRankTable.getPSSI())
                .rdi(smartRankTable.getRDI())
                .source(smartRankTable.getSource())
                .area(Double.parseDouble(smartRankTable.getArea()))
                .build();
    }

    public void deleteAll() {
        smartRankTableDao.deleteAll();
    }
}
